package cn.edu.csust.coursecard.controller;

import cn.edu.csust.coursecard.common.CodeEnum;
import cn.edu.csust.coursecard.common.ReturnData;
import cn.edu.csust.coursecard.dao.VersionDAO;
import cn.edu.csust.coursecard.dao.entity.Version;
import cn.edu.csust.coursecard.exception.BaseException;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zsw
 * @date 2020/02/16 23:02
 */
@RestController
public class VersionController {

    @Autowired
    VersionDAO versionDAO;

    @Value("${my.host}")
    String host;

    @Value("${my.port}")
    String port;

    /**
     * apk存放目录：/home/zyh/course_card/apk
     * 路径：http://101.34.59.239/apk/长理教务v3.2.5.apk
     */
    @GetMapping("/getLastVersion")
    public ReturnData getLastVersion(String form, Byte flag) {
        if ("apk".equals(form) || "ipa".equals(form)) {
            Version lastVersion = versionDAO.getLastVersion(form, flag);
            if (lastVersion == null) {
                return ReturnData.fail(CodeEnum.REQUEST_FAILED.getCode(), "暂无apk下载");
            }
            // 有外链地址则优先使用外链
            if (!lastVersion.getUrl().isEmpty()) {
                lastVersion.setApkPath(lastVersion.getUrl());
                return ReturnData.success(lastVersion);
            } else {
                // 备用下载地址
                lastVersion.setApkPath("http://" + host + "/app/" + lastVersion.getApkPath());
                return ReturnData.success(lastVersion);
            }
        } else {
            throw new BaseException(CodeEnum.PARAM_ERROR.getCode(), "参数错误！");
        }
    }

//    @PostMapping("/setNewVersion")
//    @RequiredAdmin
//    public ReturnData setNewVersion(MultipartFile file, String info, Byte flag) {
//        String name = file.getOriginalFilename();
//        if (name.contains(".apk") || name.contains(".ipa")) {
//            try {
//                qiNiuFileUtil.uploadFileToQiniu(file, "app/" + name);
//            } catch (Exception e) {
//                throw new BaseException(CodeEnum.REQUEST_FAILED.getCode(), "文件上传失败！");
//            }
//            Version version = new Version();
//            version.setInfo(info);
////            version.setUpdateTime(new Date());
//            version.setApkPath(name);
//            int index = name.lastIndexOf(".");
//            String form = name.substring(index + 1);
//            version.setForm(form);
//            version.setFlag(flag);
//            int row = versionDAO.insertVersion(version);
//            if (row != 1) {
//                throw new BaseException(CodeEnum.REQUEST_FAILED.getCode(), "参数错误！");
//            }
//            return ReturnData.success();
//        } else {
//            throw new BaseException(CodeEnum.PARAM_ERROR.getCode(), "参数错误！");
//        }
//    }

    @PostMapping("/setUrlUpdate")
    public ReturnData setExternalUpdate(String url, String info, Byte flag) {
        String apkPath = FilenameUtils.getName(url);
        String ext = FilenameUtils.getExtension(url);
        if (ext.equals("apk") || ext.equals("ipa")) {
            Version version = new Version();
            version.setInfo(info);
            version.setUrl(url);
            version.setApkPath(apkPath);
            version.setForm(ext);
            version.setFlag(flag);
            int row = versionDAO.insertVersionV2(version);
            if (row != 1) {
                throw new BaseException(CodeEnum.REQUEST_FAILED.getCode(), "参数错误！");
            }
            return ReturnData.success();
        } else {
            throw new BaseException(CodeEnum.PARAM_ERROR.getCode(), "参数错误！");
        }
    }
}
